from .base import db
from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, Date
from sqlalchemy.orm import relationship
import json

class Employee(db.Model):
    __tablename__ = "employee"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    number = db.Column(db.String(50))
    entry_time = db.Column(db.String(20))
    department = db.Column(db.String(100))
    gender = db.Column(db.String(10))
    birth_date = db.Column(db.String(20))
    ethnicity = db.Column(db.String(50))
    native_place = db.Column(db.String(100))
    birth_place = db.Column(db.String(100))
    political_status = db.Column(db.String(50))
    marital_status = db.Column(db.String(50))
    health_status = db.Column(db.String(50))
    phone = db.Column(db.String(20))
    email = db.Column(db.String(100))
    school = db.Column(db.String(100))
    major = db.Column(db.String(100))
    degree = db.Column(db.String(50))
    academic_degree = db.Column(db.String(50))
    graduation_date = db.Column(db.String(20))
    language_level = db.Column(db.String(50))
    title = db.Column(db.String(100))
    position_start_date = db.Column(db.String(20))
    current_company = db.Column(db.String(100))
    position = db.Column(db.String(100))
    contract_period_start = db.Column(db.String(20))
    contract_period_end = db.Column(db.String(20))
    current_address = db.Column(db.String(200))
    id_number = db.Column(db.String(20))
    household_address = db.Column(db.String(200))
    emergency_contact = db.Column(db.String(100))
    emergency_phone = db.Column(db.String(20))
    awards = db.Column(db.Text)
    skills = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    projects = db.relationship('Project', backref='employee', lazy=True, cascade='all, delete-orphan')
    work_experiences = db.relationship('WorkExperience', backref='employee', lazy=True, cascade='all, delete-orphan')
    educations = db.relationship('Education', backref='employee', lazy=True, cascade='all, delete-orphan')
    reward_punishments = db.relationship('RewardPunishment', backref='employee', lazy=True, cascade='all, delete-orphan')

    def to_dict(self):
        data = {
            'id': self.id,
            'name': self.name,
            'number': self.number,
            'entry_time': self.entry_time,
            'department': self.department,
            'gender': self.gender,
            'birth_date': self.birth_date,
            'ethnicity': self.ethnicity,
            'native_place': self.native_place,
            'birth_place': self.birth_place,
            'political_status': self.political_status,
            'marital_status': self.marital_status,
            'health_status': self.health_status,
            'phone': self.phone,
            'email': self.email,
            'school': self.school,
            'major': self.major,
            'degree': self.degree,
            'academic_degree': self.academic_degree,
            'graduation_date': self.graduation_date,
            'language_level': self.language_level,
            'title': self.title,
            'position_start_date': self.position_start_date,
            'current_company': self.current_company,
            'position': self.position,
            'contract_period_start': self.contract_period_start,
            'contract_period_end': self.contract_period_end,
            'current_address': self.current_address,
            'id_number': self.id_number,
            'household_address': self.household_address,
            'emergency_contact': self.emergency_contact,
            'emergency_phone': self.emergency_phone,
            'awards': self.awards,
            'skills': self.skills,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        }

        # 解析 language_level 字段（如果存在且是字符串）
        if 'language_level' in data and isinstance(data['language_level'], str):
            try:
                data['language_level'] = json.loads(data['language_level'])
            except json.JSONDecodeError:
                # 解析失败，设置为 None 或者空列表
                data['language_level'] = [] # 或者 None

        # 确保 language_level 始终是列表类型返回
        if 'language_level' not in data or data['language_level'] is None:
            data['language_level'] = []

        return data 